package middleware

import (
	"net/http"

	"codeberg.org/vnpower/pixivfe/v2/i18n"
	"codeberg.org/vnpower/pixivfe/v2/server/session"
)

// SetLocaleFromCookie is a middleware that extracts the user's locale preference
// from a cookie and sets it in the i18n package.
func SetLocaleFromCookie(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		locale := session.GetCookie(r, session.Cookie_Locale)

		i18n.SetLocale(locale)

		h.ServeHTTP(w, r)
	})
}
